package com.example.myworkc;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
import java.util.Map;

public class Myadapter extends RecyclerView.Adapter <Myadapter.MyViewHolder> {
    private View itemView;
    private Context context;
    private List<Map<String,Object>> items;
    public Myadapter(List<Map<String,Object>> items,Context context){
        this.context=context;
        this.items=items;
    }

    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        itemView=LayoutInflater.from(context).inflate(R.layout.item,parent,false);
        MyViewHolder myViewHolder=new MyViewHolder(itemView);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.textView1.setText(items.get(position).get("name").toString());
        holder.textView2.setText(items.get(position).get("words").toString());
        holder.imageView.setImageResource(Integer.parseInt(items.get(position).get("png").toString()));
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context,MainActivity2.class);
                intent.putExtra("name",items.get(holder.getAdapterPosition()).get("name").toString());
                intent.putExtra("words",items.get(holder.getAdapterPosition()).get("words").toString());
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textView1,textView2;
        ImageView imageView;
        LinearLayout linearLayout;
        Button button;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            textView1=itemView.findViewById(R.id.textView1);
            textView2=itemView.findViewById(R.id.textView2);
            imageView=itemView.findViewById(R.id.imageView);
            linearLayout=itemView.findViewById(R.id.rootView);
            //button=itemView.findViewById(R.id.button);
        }
    }
}

